package cm.mefobe.android.telecomcost.entities;

import java.util.ArrayList;

import cm.mefobe.android.telecomcost.TelecomCost;

public class DayRangeThreshold {

	public static final String PROP_TYPE = "DayRangeThreshold";
	protected double _amount;
	protected DayRange _range;
	private long _id = -1;
	protected double calculatedAmount;

	public DayRangeThreshold(double amount, DayRange _range) {
		setAmount(amount);
		setDayRange(_range);
	}
	
	public DayRangeThreshold(DayRange _range) {
		setAmount(0);
		setDayRange(_range);
	}
	
	public ArrayList<DailyThreshold> getDailyThresholds()
	{
		ArrayList<DailyThreshold> thresholds = new ArrayList<DailyThreshold>();
		ArrayList<Day> days = _range.getDays();
		
		int index = 0;
		while(index < days.size())
		{
			DailyThreshold threshold = TelecomCost.getDatabaseManager().getDayThreshold(days.get(index));
			calculatedAmount = calculatedAmount + threshold.getAmount();
			thresholds.add(threshold);
			index++;
		}
		
		return thresholds;
	}
	
	public double getCalculatedAmount() {
		return calculatedAmount;
	}

	public DayRangeThreshold() {
		setAmount(0);
		setDayRange(new DayRange(Day.today(),Day.today()));
	}

	public DayRangeThreshold(double d) {
		setAmount(d);
		setDayRange(new DayRange(Day.today(),Day.today()));
	}

	private boolean selected;
	private String _type;

	public boolean isSelected() {
		return selected;
	}

	public void setSelected(boolean value) {
		selected = value;
	}

	public void setAmount(double d) {
		this._amount = d;
	}

	public double getAmount() {
		return _amount;
	}

	public void setDayRange(DayRange range) {
		this._range = range;
	}	

	public DayRange getDayRange() {
		return _range;
	}

	public String getType(){
		return _type;
	}
	
	public void setType(String type){
		_type = type;
	}

	public void setId(long _id) {
		this._id = _id;
	}

	@Override
	public int hashCode() {
		return _range.getFirstDay().hashCode() + _range.getLastDay().hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		DayRangeThreshold other = (DayRangeThreshold) obj;
		if (Double.doubleToLongBits(_amount) != Double
				.doubleToLongBits(other._amount))
			return false;
		if (_id != other._id)
			return false;
		if (_range == null) {
			if (other._range != null)
				return false;
		} else if (!_range.equals(other._range))
			return false;
		if (_type == null) {
			if (other._type != null)
				return false;
		} else if (!_type.equals(other._type))
			return false;
		if (Double.doubleToLongBits(calculatedAmount) != Double
				.doubleToLongBits(other.calculatedAmount))
			return false;
		if (selected != other.selected)
			return false;
		return true;
	}

	public long getId() {
		return _id;
	}
}
